package com.firetornado.android.gowallajava;

import com.firetornado.android.gowallajava.ImageOps;
import com.firetornado.android.gowallajava.R;
import com.firetornado.android.gowallajava.GowallaJavaAuth;
import com.ginsberg.gowalla.dto.FullSpot;
import com.ginsberg.gowalla.exception.GowallaException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This is an example of how to use gowalla-java within Android to call and display spot details.
 * 
 * @author Ryan Crutchfield - rc6750@gmail.com
 * http://gowalla.com/users/rc6750
 *
 */
public class SpotsMain extends Activity {
	
	final Handler mHandler = new Handler(); 
	FullSpot spotData = null;
		
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.spotsmain);
        final ProgressDialog pd = ProgressDialog.show(this,"Spot Details","Loading....",true, false);
   		new Thread(new Runnable(){
       		public void run(){
       			try {
       	        	spotData = GowallaJavaAuth.gowalla.getSpot(11888);
       			} catch (GowallaException e) {
       				// TODO Auto-generated catch block
       				e.printStackTrace();
       			}
       	    	mHandler.post(mUpdateResults);
        		pd.dismiss();
       		}
   		}).start();
     }
		
	final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi(spotData);
        }
    }; 
    
    final void updateResultsInUi(FullSpot spotData)
    {
    	Drawable image = ImageOps.ImageOperations(spotData.getImageUrl(),"image.jpg");		
    	ImageView imgView = (ImageView)findViewById(R.id.image1);
		imgView.setImageDrawable(image);
    			
		TextView spotName = (TextView)findViewById(R.id.spotName);
		spotName.setText(spotData.getName());
		
		TextView spotDescription = (TextView)findViewById(R.id.spotDescription);
		spotDescription.setText(spotData.getDescription());
		
		TextView usersCount = (TextView)findViewById(R.id.usersCount);
		usersCount.setText("People: " +Integer.toString(spotData.getUsersCount()));
		
		TextView checkIns = (TextView)findViewById(R.id.checkIns);
		checkIns.setText("Check-ins: " + Integer.toString(spotData.getCheckinsCount()));
	}
	
}